package com.yuyou.tas.terminal.comm;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yuyou.tas.util.tool.HttpClient;
import com.yuyou.tas.util.tool.LogUtil;
import com.yuyou.tas.util.tool.MD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 教师端接口
 *
 */
@Service
public class YyfnTAPI {
		
	@Value("${yyfnAddr}")
	public  String yyfnAddr;

	@Value("${yyfn.appId}")
	public  String appId;

	@Value("${yyfn.secret_key}")
	public  String secret_key;

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * openid登陆
	 */
	public  static String GET_TEACHER_TOKEN="/tas/findOpenIdByTeacherId";
	
	
	public List<String> getTeacherDetail(Long teacherId){
		TeacherTokenVo teacherTokenVo= null;
		try {
			Long times = new Date().getTime();

			Map<String, String> params=new HashMap<>();
			params.put("teacherId", String.valueOf(teacherId));

			Map<String, String> header=new HashMap<>();
			String sign = MD5Util.md5Hex(appId+secret_key+times).toLowerCase();
			header.put("appId", appId);
			header.put("times", String.valueOf(times));
			header.put("sign", sign);

			String res =HttpClient.post(yyfnAddr+YyfnTAPI.GET_TEACHER_TOKEN, params,header);

			LogUtil.info(logger,"获取教师openID，返回：" + res);
			teacherTokenVo = JSONObject.parseObject(res, new TypeReference<TeacherTokenVo>() {});
		} catch (Exception e) {
			LogUtil.error(logger , "获取教师openID异常：" ,e);
		}

		if(teacherTokenVo != null && teacherTokenVo.isSuccess() && !CollectionUtils.isEmpty(teacherTokenVo.getData())){
			return teacherTokenVo.getData();
		}
		return null;
	}
	
	
}
